package org.iplugin.version.handler;

import org.iplugin.version.model.IModifiedPomVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;

/**
 * 　　* @Description: maven model version update
 * 　　* @author shangyouzhi1
 * 　　* @date 2022/11/27 14:57
 *
 */
public class IVersionUpdateModelHandler extends IVersionUpdateAbstractHandler {

    public IVersionUpdateModelHandler() {
        this.setNextHandler(new IVersionUpdateParentHandler());
    }

    @Override
    public void updateVersion(IModifiedPomVersion modifiedPomVersion) {
        this.getLog().info("开始更新model version");
        Model model = modifiedPomVersion.getModel();
        String version = model.getVersion();
        String modelVersion = newVersionMap.get(model.getGroupId() + ":" + model.getArtifactId());
        if (StringUtils.isNotBlank(modelVersion) && !StringUtils.equals(version, modelVersion)) {
            if (StringUtils.isNotBlank(version)) {
                String newModelVersion = newVersionMap.get(model.getGroupId() + ":" + model.getArtifactId());
                this.getLog().info("更新model版本号：" + model.getGroupId() + ":" + model.getArtifactId() + "更新前：" + version + ",更新后：" + newModelVersion);
                model.setVersion(newModelVersion);
                modifiedPomVersion.modifiedVersion();
            }
        }
        if (getNextHandler() != null) {
            getNextHandler().updateVersion(modifiedPomVersion);
        }
    }
}
